package blog.service.impl;

import blog.common.Result;
import blog.entity.Article;
import blog.entity.Login;
import blog.mapper.ArticleMapper;
import blog.mapper.LoginMapper;
import blog.service.ArticleService;
import blog.service.LoginService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jjpzh
 * @since 2023-02-25
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Override
    public Result findAllArticle(int current, int size) {
        Page<Article> page = new Page<>(current,size);
        //按照创建时间降序排列
        Page<Article> articlePage = baseMapper.selectPage(page,new LambdaQueryWrapper<Article>().orderByDesc(Article::getCreateTime));


        return Result.success(articlePage);
    }

    @Override
    public Result findArticleByType(int current, int size, int typeId) {
        Page<Article> page = new Page<>(current,size);

        //按照创建时间降序排列
        Page<Article> articlePage = baseMapper.selectPage(page,new LambdaQueryWrapper<Article>().eq(Article::getTypeId,typeId).orderByDesc(Article::getCreateTime));
        return Result.success(articlePage);
    }

    @Override
    public Result addArticleVisitCount(int id) {
        Article article = baseMapper.selectById(id);
        if (article==null){
            return Result.error("文章不存在");
        }
        article.setVisits(article.getVisits()+1);
        baseMapper.updateById(article);
        return Result.success();
    }

    @Override
    public Result findArticleByTitle(int current, int size, String title) {
        Page<Article> page = new Page<>(current,size);
        Page<Article> articlePage = baseMapper.selectPage(page,new LambdaQueryWrapper<Article>().like(Article::getTitle,title).orderByDesc(Article::getCreateTime));
        return Result.success(articlePage);
    }

    @Override
    public Result findArticleFromAtoB(int a, int b) {
        List<Article> articles = baseMapper.selectList(new LambdaQueryWrapper<Article>().between(Article::getId, a, b).orderByDesc(Article::getCreateTime));
        return Result.success(articles);

    }




}
